package cn.hd01.weixin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hd01.repository.entity.COrder;
import cn.hd01.repository.entity.COrderPayment;
import cn.hd01.repository.entity.Customer;
import cn.hd01.repository.entity.OrderProduct;
import cn.hd01.service.COrderPaymentService;
import cn.hd01.service.COrderService;
import cn.hd01.service.CustomerService;
import cn.hd01.service.OrderProductService;
import cn.hd01.web.auth.Auth;
import cn.hd01.web.auth.AuthType;
import cn.hd01.web.util.WebHelper;

@Controller
@RequestMapping("/weixin/order")
@Auth(AuthType.WECHAT)
public class WXOrderController {

	@Autowired
	private CustomerService cusomterService;

	@Autowired
	private COrderService orderService;
	
	@Autowired
	private OrderProductService orderProductService;
	
	@Autowired
	private COrderPaymentService corderPaymentService;
	
	@RequestMapping(path = "/tolist", method = RequestMethod.GET)
	public String customerlist(String menu, COrder order, Model m) {
		m.addAttribute("menu", menu);
		m.addAttribute("customerId", order.getCustomerId());
		m.addAttribute("distributorId", order.getDistributorId());
		m.addAttribute("isBalance", order.getIsBalance());
		m.addAttribute("beginDate", order.getBeginDate());
		m.addAttribute("endDate", order.getEndDate());
		return "weixin/order";
	}

	@RequestMapping(path = "/toRebateBook", method = RequestMethod.GET)
	public String customerlist(COrder order, Model m) {
		Customer customer = cusomterService.findByOauthId(WebHelper.currentWXOauth().getId());
		m.addAttribute("recommDistributorId", customer.getId());
		m.addAttribute("distributorId", order.getDistributorId());
		m.addAttribute("beginDate", order.getBeginDate());
		m.addAttribute("endDate", order.getEndDate());
		return "weixin/supplier/rebateBook";
	}
	
	@ResponseBody
	@RequestMapping(path = "/list", method = RequestMethod.GET)
	public Page<COrder> customerlist(COrder order, @PageableDefault Pageable pageable) {
		Sort sort = new Sort(Direction.DESC, "createTime");
		Pageable page = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
		Page<COrder> result = orderService.findAll(order, page);
		/*if(!CollectionUtils.isEmpty(result.getContent())){
			for(COrder co : result.getContent()){
				List<OrderProduct> productList = orderProductService.findByOrderIdOrderByPayTimeDesc(co.getId());
				StringBuffer sb = new StringBuffer();
				if(!CollectionUtils.isEmpty(productList)){
					for(OrderProduct product : productList){
						sb.append(product.getProductName()).append(",");
					}
					co.setProductName(sb.substring(0, sb.length() -1));
				}
				
			}
		}*/
		return result;
	}
	
	@ResponseBody
	@RequestMapping(path = "/kValueList", method = RequestMethod.GET)
	public Page<Object[]> kValueList(COrder order, @PageableDefault Pageable pageable) {
		return orderService.findKValueList(order, pageable);
	}
	
	@RequestMapping(path = "/paymentList", method = RequestMethod.GET)
	public String paymentList(Integer orderId, Model m) {
		List<COrderPayment> list = corderPaymentService.findByOrderId(orderId);
		if(!CollectionUtils.isEmpty(list)){
			for(COrderPayment payment : list){
				payment.setAmount(payment.getAmount().subtract(payment.getFeeAmount()));
			}
		}
		m.addAttribute("list", list);
		return "weixin/supplier/orderPayment";
	}

}
